import time

import pymysql
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

import TalabatConfig

chrome_driver_instance = None


def getDatabaseConnection():
    mysqlConnection = pymysql.connect(
        host=TalabatConfig.talabatConfig['talabat']['db']['host'],
        port=TalabatConfig.talabatConfig['talabat']['db']['port'],
        user=TalabatConfig.talabatConfig['talabat']['db']['user'],
        password=TalabatConfig.talabatConfig['talabat']['db']['pass'],
        database=TalabatConfig.talabatConfig['talabat']['db']['dbName'])

    return mysqlConnection


def insert(restaurant, rating, url):
    try:
        _connection = getDatabaseConnection()

        _cursor = _connection.cursor()

        _cursor.execute("insert into talabat_restaurants(restaurant, rating, url) values ('"
                        + restaurant + "', " + str(rating) + ", '" + url + "')")

        _connection.commit()

    except Exception as e:
        if _connection:
            _connection.rollback()
    finally:
        if _cursor:
            _cursor.close()
        if _connection:
            _connection.close()


def createChromeDriver():
    global chrome_driver_instance

    if chrome_driver_instance:
        return

    try:
        s = Service(executable_path='/Users/gilbert/chromedriver-mac-x64/chromedriver')
        chrome_driver_instance = webdriver.Chrome(service=s)

        chrome_driver_instance.maximize_window()
    except Exception as e:
        print(e.__str__())


def closeChromeDriver():
    chrome_driver_instance.close()


def scrapyRestaurantRating():
    createChromeDriver()

    page = int(TalabatConfig.talabatConfig['talabat']['startPage'])

    endPage = int(TalabatConfig.talabatConfig['talabat']['endPage'])

    while True:
        if page > endPage:
            break

        print("当前爬取页：" + str(page))

        actualRestaurants = traversePage(page)

        filterRestaurants(actualRestaurants)

        if len(actualRestaurants) < int(TalabatConfig.talabatConfig['talabat']['page4Items']):
            break

        page = page + 1

    closeChromeDriver()


def traversePage(page):
    chrome_driver_instance.get("https://www.talabat.com/uae/restaurants?page=" + str(page))

    time.sleep(4)

    restaurants = []

    urls = chrome_driver_instance.find_elements(By.XPATH, "//div[@data-testid='vendor']/a")
    for url in urls:
        restaurant = url.get_attribute('href')
        restaurants.append(restaurant)

    return restaurants


def filterRestaurants(actualRestaurants):
    for restaurantUrl in actualRestaurants:
        chrome_driver_instance.get(restaurantUrl)
        time.sleep(3)

        try:
            rating = chrome_driver_instance.find_element(By.XPATH, "//div[@data-testid='brand-rating-number']").text
            restaurant = restaurantUrl.split("/")[-1]
            print(restaurant + "/" + rating + "/" + restaurantUrl)
            insert(restaurant, float(rating), restaurantUrl)

        except Exception as e:
            print(e.__str__())


if __name__ == '__main__':
    scrapyRestaurantRating()
